package com.javabi.common.lang.clazz;

import java.lang.reflect.Modifier;

import com.google.common.base.Throwables;

/**
 * A Class Utility.
 */
public class ClassUtil {

	/**
	 * Returns the class for the given name.
	 * @param className the class name.
	 * @return the class for the given name.
	 */
	public static final Class<?> forName(String className) {
		try {
			return Class.forName(className);
		} catch (ClassNotFoundException e) {
			throw Throwables.propagate(e);
		}
	}

	/**
	 * Returns a new instance of the class.
	 * @param clazz the class.
	 * @return a new instance of the class.
	 */
	public static final <T> T newInstance(Class<T> clazz) {
		try {
			return clazz.newInstance();
		} catch (Exception e) {
			throw Throwables.propagate(e);
		}
	}

	/**
	 * Returns a new instance of the named class.
	 * @param className the class name.
	 * @return a new instance of the namedclass.
	 */
	public static final Object newInstance(String className) {
		Class<?> clazz = forName(className);
		if (clazz.isInterface()) {
			throw new IllegalArgumentException("Class is interface: " + clazz);
		}
		if (clazz.isPrimitive()) {
			throw new IllegalArgumentException("Class is primitive: " + clazz);
		}
		if (Modifier.isAbstract(clazz.getModifiers())) {
			throw new IllegalArgumentException("Class is abstract: " + clazz);
		}
		return newInstance(clazz);
	}
}
